﻿var app = angular.module('app', ['angular.jquery']);
var item = null;
var flag = false;
app.controller('DemoCtrl', [
    '$scope','$http',
    'TestDialogService',
function ($scope,$http, TestDialogService) {
    'use strict';        
    $http.get(Editor.Url + "/vu/GetVuDetails")
     .success(function (response) {
         $scope.talks = response;
         console.dir(response);
     });
     
    $scope.loadData = function () {
        $http.get(Editor.Url + "/vu/GetVuDetails")
                .success(function (response) {
                    $scope.talks = response;
         });
    }

    $scope.delete = function (id){
       var a = confirm("Bạn có thực sự muốn xóa dòng này?");
        if (a === false)
            return;
        $http.delete(Editor.Url + "/api/VuAPI/" + id).success(function (data, status, headers) {
            $scope.loadData();
        });
    }
   
    $scope.testDialog = function (_flag, _item) {
        item = _item;
        flag = _flag;
        TestDialogService.openDialog({
            testValue: 'test'
            }).then(function (result) {
                if (result.ok) {
                    if (flag) {
                        $http.put(Editor.Url + "/api/VuAPI/" + result.obj.iD, result.obj).success(function (data, status, headers) {
                            if (status === 200) {
                                alert("cập nhập thành công");
                            }
                        });
                    }
                    else {
                        $http.post(Editor.Url + "/api/VuAPI", result.obj).success(function (data, status, headers) {
                            $scope.loadData();
                        });
                    }
              }
        });

    };

}]);



app.controller('DialogCtrl', [
    '$scope',
    'TestDialogService',
function($scope, TestDialogService) {
    'use strict';

    $scope.vu = null;
    $scope.onOpen = function () {
        if (flag)
            $scope.vu = item;
        else {

        }
    };

    $scope.$parent.onOk = function () {        
        TestDialogService.closeDialog({ ok: true, obj: $scope.vu});
    };

    $scope.onCancel = function() {
        TestDialogService.closeDialog({ok: false});
    };

    $scope.beforeClose = function() {
        return "";//confirm('Really close?')
    };

}]);